package com.lmh.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lmh.entity.Channel;
import com.lmh.mapper.ChannelDao;
import com.lmh.utils.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChannelService {

    @Autowired
    private ChannelDao channelDao;

    public int create(Channel channel) {
        return channelDao.create(channel);
    }

    public int delete(Integer id) {
       return channelDao.delete(Maps.build(id).getParamMap());
    }

    public int update(Channel channel) {
        return channelDao.update(Maps.build(channel.getId()).beanToMapForUpdate(channel));
    }

    public PageInfo<Channel> query(Channel channel) {
        if(channel != null && channel.getPage() != null) {
            PageHelper.startPage(channel.getPage(), channel.getLimit());
        }
        List<Channel> list = channelDao.query(Maps.build().beanToMap(channel));
        return new PageInfo<Channel>(list);
    }

    public List<Channel> getChannelsByPos(String pos) {
        return channelDao.query(Maps.build().add("pos", pos).getParamMap());
    }

    // 返回所有数据
    public List<Channel> tree() {
        return channelDao.tree();
    }

    public Channel detail(Integer id) {
        return channelDao.detail(Maps.build(id).getParamMap());
    }

    public int count(Channel channel) {
        return channelDao.count(Maps.build().beanToMap(channel));
    }

}
